package com.zhugang.week04;

import java.util.HashMap;
import java.util.Map;

/**
 * @program algorithms
 * @description: fib
 * @author: chanzhugang
 * @create: 2022/06/23 19:04
 */
public class Fib2 {

    /**
     * 斐波那契数列
     *
     * @param n
     * @return
     */
    Map<Integer, Integer> cacheMap = new HashMap<>();
    int mod = 1000000007;

    public int fib(int n) {
        if (n == 0) {
            return 0;
        }
        if (n == 1) {
            return 1;
        }
        if (cacheMap.containsKey(n)) {
            return cacheMap.get(n);
        }
        int res = (fib(n - 1) + fib(n - 2)) % mod;
        cacheMap.put(n, res);
        return res;
    }
}